package com.weaver.view;

import com.weaver.controller.Controller;
import com.weaver.model.Model;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * BoardPanel class - Displays the game board
 */
public class BoardPanel extends JPanel {
    private static final int MAX_ATTEMPTS = 10;
    private static final int WORD_LENGTH = 4;
    
    private Model model;
    private JLabel[][] letterLabels;
    
    /**
     * Constructor
     * @param model game model
     */
    public BoardPanel(Model model) {
        this.model = model;
        
        // Set layout
        setLayout(new GridLayout(MAX_ATTEMPTS, WORD_LENGTH, 5, 5));
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // Create letter labels
        letterLabels = new JLabel[MAX_ATTEMPTS][WORD_LENGTH];
        for (int row = 0; row < MAX_ATTEMPTS; row++) {
            for (int col = 0; col < WORD_LENGTH; col++) {
                JLabel label = new JLabel();
                label.setPreferredSize(new Dimension(50, 50));
                label.setHorizontalAlignment(SwingConstants.CENTER);
                label.setVerticalAlignment(SwingConstants.CENTER);
                label.setFont(new Font("Arial Black", Font.BOLD, 20));
                label.setOpaque(true);
                label.setBackground(Color.LIGHT_GRAY);
                label.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
                
                letterLabels[row][col] = label;
                add(label);
            }
        }
        
        // Initialize first row as start word
        String startWord = model.getStartWord();
        for (int col = 0; col < WORD_LENGTH; col++) {
            letterLabels[0][col].setText(String.valueOf(startWord.charAt(col)));
        }
    }
    
    /**
     * Update the game board
     */
    public void updateBoard() {
        // Get attempts list and feedbacks
        List<String> attempts = model.getAttempts();
        List<String[]> feedbacks = model.getFeedbacks();
        
        // Clear all labels
        for (int row = 0; row < MAX_ATTEMPTS; row++) {
            for (int col = 0; col < WORD_LENGTH; col++) {
                letterLabels[row][col].setText("");
                letterLabels[row][col].setBackground(Color.LIGHT_GRAY);
            }
        }
        
        // Set start word
        String startWord = model.getStartWord();
        for (int col = 0; col < WORD_LENGTH; col++) {
            letterLabels[0][col].setText(String.valueOf(startWord.charAt(col)));
        }
        
        // Set attempts and feedbacks
        for (int row = 0; row < attempts.size(); row++) {
            String attempt = attempts.get(row);
            String[] feedback = feedbacks.get(row);
            
            for (int col = 0; col < WORD_LENGTH; col++) {
                letterLabels[row + 1][col].setText(String.valueOf(attempt.charAt(col)));
                
                // Set color
                if (feedback[col].equals("correct")) {
                    letterLabels[row + 1][col].setBackground(new Color(106, 170, 100)); // Green
                    letterLabels[row + 1][col].setForeground(Color.WHITE);
                } else {
                    letterLabels[row + 1][col].setBackground(Color.GRAY); // Gray
                    letterLabels[row + 1][col].setForeground(Color.WHITE);
                }
            }
        }
        
        // If there is current input, display it on the next row
        if (model.getAttemptsCount() < MAX_ATTEMPTS - 1) {
            int nextRow = model.getAttemptsCount() + 1;
            String currentInput = "";
            
            // Try to get current input from model's arg
            Object arg = model.getLastNotificationArg();
            if (arg instanceof String) {
                currentInput = (String) arg;
                System.out.println("Current input from model arg: " + currentInput); // Debug output
            }
            
            // If we couldn't get it from model, try to find controller
            if (currentInput.isEmpty()) {
                // Search for View in parent hierarchy
                Container parent = getParent();
                while (parent != null && !(parent instanceof View)) {
                    parent = parent.getParent();
                }
                
                if (parent instanceof View) {
                    View view = (View) parent;
                    Controller controller = view.getController();
                    if (controller != null) {
                        currentInput = controller.getCurrentInput();
                        System.out.println("Current input from controller: " + currentInput); // Debug output
                    }
                }
            }
            
            // Display current input with appropriate background color
            for (int col = 0; col < WORD_LENGTH; col++) {
                if (col < currentInput.length()) {
                    letterLabels[nextRow][col].setText(String.valueOf(currentInput.charAt(col)));
                    
                    // Set background color based on input status
                    if (arg instanceof Boolean) {
                        boolean isError = (Boolean) arg;
                        if (isError) {
                            // Red background for error
                            letterLabels[nextRow][col].setBackground(new Color(220, 100, 100));
                            letterLabels[nextRow][col].setForeground(Color.WHITE);
                        } else {
                            // Light green background for valid input
                            letterLabels[nextRow][col].setBackground(new Color(200, 230, 200));
                        }
                    } else {
                        // Default color for normal input
                        letterLabels[nextRow][col].setBackground(new Color(240, 240, 240));
                    }
                } else {
                    letterLabels[nextRow][col].setText("");
                    letterLabels[nextRow][col].setBackground(Color.LIGHT_GRAY);
                }
            }
        }
    }
}